package cc.abanlee.framework.security.core.service;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: create by
 * @version: v1.0
 * @description:
 * @date:2022/8/29
 */
@Service
public class CustomUserDetailsService implements UserDetailsService {
    /**
     * 需新建配置类注册一个指定的加密方式Bean，或在下一步Security配置类中注册指定
     */
    //@Resource
    private PasswordEncoder passwordEncoder;

    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 角色集合
        List<GrantedAuthority> authorities = new ArrayList<>();
        // 角色必须以`ROLE_`开头，数据库中没有，则在这里加
        authorities.add(new SimpleGrantedAuthority( "ROLE_user"));
       // authorities.add(new SimpleGrantedAuthority( "ROLE_admin"));

        return new User("zhangsan",passwordEncoder.encode("$2a$10$QY0/hXe1ksYabJEZGvJpou2OnX1g/wZy9nwN3kMPxaGxwPi7NBCZC"),authorities);
    }
}
